package thread_base.thread_safe;

import annotation.ThreadSafe;

import java.math.BigInteger;

// *
// Written by MAX 2024/7/25 10:31
// Note:*
@ThreadSafe
public class SynchronizedFactorizer {
    private BigInteger lastNumber;
    private BigInteger[] lastFactors;

    public synchronized void service(ServletRequest req, ServletResponse resp) {
        BigInteger i = extractFromRequest(req);
        if (i.equals(lastNumber)) {
            encodeIntoResponse(resp, lastFactors);
        }else {
            BigInteger[] factors = factor(i);
            lastNumber = i;
            lastFactors = factors;
            encodeIntoResponse(resp, factors);
        }

    }

    BigInteger[] factor(BigInteger i) {
        // Doesn't really factor
        return new BigInteger[]{i};
    }

    BigInteger extractFromRequest(ServletRequest req) {
        return new BigInteger("7");
    }

    void encodeIntoResponse(ServletResponse resp, BigInteger[] factors) {
    }
}
